package mercede.allManagerBeans;
import mercede.allManagers.StandardpkManager;
import mercede.allTable.Standardpk;
import mercede.allDTOs.StandardpkDTO;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;




/**
 * Session Bean implementation class StandardpkManagerBean
 */
@Stateless
public class StandardpkManagerBean implements StandardpkManager {
    

	@PersistenceContext
   private EntityManager em;
	
	@Resource
	private EJBContext context;
	

    @Override
    public void save(StandardpkDTO standardpkdto) {
            Standardpk newStandardpk = new Standardpk(standardpkdto);
            em.persist(newStandardpk);
    }
    
       
    @Override
    public void remove(int sid) {
 		Standardpk standardpk = find(sid);
 		System.out.println(sid);
 		System.out.println(standardpk);
         em.remove(standardpk);
 	}
     
     public void remove(Standardpk standardpk) {
     	em.remove(standardpk);
 	}
     
   
   private Standardpk find(int sid) {
   return em.find(Standardpk.class, sid);
   }
    
   @Override
   public ArrayList<?> getStandardpk() {
   return (ArrayList<?>) em.createNamedQuery("Standardpk.FIND_ALL", Standardpk.class).getResultList();
   }
   
   
   @Override
   public StandardpkDTO getStandardpkDTO(int sid) {
	   Standardpk newStandardpk =find(sid);
	   StandardpkDTO standardpkdto = convertToDTO(newStandardpk);
       return standardpkdto;
   }
   

   @Override
   public List<StandardpkDTO> getListStandardpkDTO() {
       List<Standardpk> newStandardpk= em.createNamedQuery("Standardpk.FIND_ALL", Standardpk.class).getResultList();
       List<StandardpkDTO> standardpkdto = new ArrayList<>();
       for(Standardpk h: newStandardpk) {
    	   standardpkdto.add(convertToDTO(h));
       }
       return standardpkdto;
   }
   
   

   @Override
   public List<StandardpkDTO> getListStandardpkDTOByDest(String scity) {
	   
      Query query=em.createNamedQuery("Standardpk.FIND_BY_DEST", Standardpk.class);
      query.setParameter("scity",scity);
	   List<Standardpk> newStandardpk= query.getResultList();
       List<StandardpkDTO> standardpkdto = new ArrayList<>();
       for(Standardpk h: newStandardpk) {
    	   standardpkdto.add(convertToDTO(h));
       }
       return standardpkdto;
   }
   
   public static List<StandardpkDTO> convertToDTOList(List<Standardpk> standardpkList) {
       List<StandardpkDTO> standardpkdtoList = new ArrayList<StandardpkDTO>();
       for(Standardpk h: standardpkList){
    	   standardpkdtoList.add(convertToDTO(h));
       }
       return  standardpkdtoList;
}   
   
   
    public String getPrincipalName() {
        return context.getCallerPrincipal().getName();
    }
    
    public static  StandardpkDTO convertToDTO(Standardpk standardpk)
    {
    	StandardpkDTO newStandardpkdto=new StandardpkDTO();
    	newStandardpkdto.setsID(standardpk.getSID());
    	newStandardpkdto.setsCity(standardpk.getSCity());
    	newStandardpkdto.setsExcursion(standardpk.getSExcursion());
    	newStandardpkdto.setsFlight(standardpk.getSFlight());
    	newStandardpkdto.setsHotel(standardpk.getSHotel());
    	newStandardpkdto.setsNofdays(standardpk.getSNofdays());
    	newStandardpkdto.setsPrice(standardpk.getSPrice());
    	
    	return newStandardpkdto;
    	
    }

}
